package com.intellagric.service.iftmonitor.Impl.ift_network;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftNetworkMapper;
import com.intellagric.pojo.IftNetwork;
import com.intellagric.pojo.IftNetworkExample;
import com.intellagric.service.iftmonitor.ift_network.NetWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 网络信息服务实现
 */
@Service
public class NetworkServiceImpl implements NetWorkService {

    @Autowired
    private IftNetworkMapper networkMapper;

    /**
     * 添加网络信息
     * @param network
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addNetWork(IftNetwork network) {
        if (networkMapper.insertSelective(network) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除网络信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteNetWork(String id) {
        if (networkMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑网络信息
     * @param network
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editNetWork(IftNetwork network) {
        if (networkMapper.updateByPrimaryKeySelective(network) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询网络信息
     * @param id
     * @return IftNetwork
     */
    @Override
    public IftNetwork getNetWorkById(String id) {
        return networkMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过设备id查询网络信息
     * @param id
     * @return IftNetwork
     */
    @Override
    public LayuiDataGridResult getNetWorkByequipmentId(String id) {
        IftNetworkExample networkExample = new IftNetworkExample();
        networkExample.createCriteria().andEquipmentIdEqualTo(id);
        List<IftNetwork> networkList = networkMapper.selectByExample(networkExample);
        return LayuiDataGridResult.success().add(networkList,networkList.size());
    }

    /**
     * 查询网络信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getNetWorkList() {
        IftNetworkExample networkExample = new IftNetworkExample();
        networkExample.createCriteria().andIdIsNotNull();
        List<IftNetwork> networkList = networkMapper.selectByExample(networkExample);
        return LayuiDataGridResult.success().add(networkList,networkList.size());
    }

    /**
     * 分页查询网络信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getNetWorkPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftNetworkExample networkExample = new IftNetworkExample();
        networkExample.createCriteria().andIdIsNotNull();
        List<IftNetwork> networkList = networkMapper.selectByExample(networkExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(networkList);
        return result;
    }
}
